package logic.cmd
{
	import common.core.GEevnt;
	import common.manager.ObserverEvt;
	
	import component.hide.FileReference2;
	import component.untils.ComponentItem;
	import component.untils.ComponentXMLAdapter;
	import component.untils.SerializeUIXML;
	
	import flash.events.Event;

	public class SaveUIXMLCMD
	{
		public static const NAME  : String = "SAVEUIXMLCMD";
		public static const SAVEUIXML : String = "SaveUIXML_SaveUIXML";
		public static const SAVECOMPLETE : String = "SaveUIXML_SaveComplete";
		
		private var m_componentXML : XML;
		private var m_uiRootFrameGroup : Vector.<ComponentItem>;
		
		private var m_rootItemPoint : int;
		
		public function SaveUIXMLCMD()
		{
			ObserverEvt.addRec(SAVEUIXML,this,onSaveUIXML);
		}
		
		/**
		 * 收到保存UI XML的消息
		 * @param evt 
		 */		
		private function onSaveUIXML(evt : GEevnt) : void
		{
			m_uiRootFrameGroup = evt.data as Vector.<ComponentItem>;
			
			m_rootItemPoint = 0;
			
			checkUIXML();
		}
		
		/**
		 * 检索UI XML 
		 */		
		public function checkUIXML() : void
		{
			m_componentXML = <Component name="" x="" y="" width="" height=""  skin="" filterName=''></Component>;
			
			if(m_uiRootFrameGroup.length > m_rootItemPoint)
			{
				if(m_uiRootFrameGroup[m_rootItemPoint])
				{
					var file:FileReference2 = new FileReference2;
					m_componentXML.@x = m_uiRootFrameGroup[m_rootItemPoint].item.x;
					m_componentXML.@y = m_uiRootFrameGroup[m_rootItemPoint].item.y;
					m_componentXML.@width = m_uiRootFrameGroup[m_rootItemPoint].item.width;
					m_componentXML.@height = m_uiRootFrameGroup[m_rootItemPoint].item.height;
					m_componentXML.@name = m_uiRootFrameGroup[m_rootItemPoint].item.name;
					m_componentXML.@filterName = m_uiRootFrameGroup[m_rootItemPoint].filterName;
					m_componentXML.@skin = m_uiRootFrameGroup[m_rootItemPoint].item.skinName;
					
					SerializeUIXML.createXML(m_uiRootFrameGroup[m_rootItemPoint],m_componentXML);
					
					file.save(m_componentXML,m_uiRootFrameGroup[m_rootItemPoint].item.name + ".xml",onSaveUIComplete);
					m_rootItemPoint ++;
				}
			}
		}
		
		private function onSaveUIComplete( ) : void
		{
			checkUIXML();
		}
		
		/**
		 * 创建UI XML
		 * @param cItem
		 * @param itemNode
		 * @param curXml 
		 */		
		private function createXML(cItem:ComponentItem,curXml:XML) : void
		{
			if(!cItem) return;
			
			var itemXML : XML;
			
			for(var i:int=0;i<cItem.childs.length;i++)
			{
				var child:ComponentItem = cItem.childs[i];
				
				if(child.item)
				{	
					itemXML = getComponentItemXML(child.item);
					
					curXml.appendChild(itemXML);
					
					if(child.childs.length > 0)
					{	
						createXML(child,itemXML);
					}
				}
			}
		}
		
		/**
		 * 获取组建对应的XML
		 * @param item
		 * @return  
		 */		
		private function getComponentItemXML(item : *) : XML
		{
			var itemXML : XML;
			var componentXMLApataer : ComponentXMLAdapter = new ComponentXMLAdapter;
			
			componentXMLApataer.target = item;
			itemXML =componentXMLApataer.getXML();
			
			var name : String = item.toString().replace("[object ","");
			name = name.replace("]","");
			
			itemXML.setLocalName(name);
			itemXML.@x = item.x;
			itemXML.@y = item.y;
			itemXML.@width = item.width;
			itemXML.@height = item.height;
			itemXML.@name = item.name;
			
			componentXMLApataer.destory();
			
			return itemXML;
		}
	}
}